package com.t2c.tibetantochinese.wordstudy;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.t2c.tibetantochinese.MainActivity;
import com.t2c.tibetantochinese.R;
import com.t2c.tibetantochinese.base.BaseFragment;
import com.t2c.tibetantochinese.common.HttpRequestManager;
import com.t2c.tibetantochinese.home.HomeFragment;
import com.t2c.tibetantochinese.moudle.FiveHundredList;
import com.t2c.tibetantochinese.moudle.WordStudy;
import com.t2c.tibetantochinese.realmModel.Phrase;
import com.t2c.tibetantochinese.utils.CommonUtil;
import com.t2c.tibetantochinese.utils.RealmUtil;
import com.t2c.tibetantochinese.views.TitleBar;

import java.util.List;

import io.realm.RealmResults;

/**
 * Created by zoulu on 16/6/20.
 * 藏文字学习
 */
public class TibetanStudy extends BaseFragment{
    private ListView listView;
    private RealmResults<Phrase> list;

    @Override
    protected int getContentViewResId() {
        return R.layout.tibetanstudy_layout;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
        initData();
    }

    private void init(View view){
        listView = (ListView) view.findViewById(R.id.listview);
        TitleBar titleBar = (TitleBar) view.findViewById(R.id.titlebar);
        if(CommonUtil.isChinese(getActivity())) {
            titleBar.setTitle(getResources().getString(R.string.study));
        }else{
            titleBar.getTitle().setTypeface(CommonUtil.getTibetanType(getActivity()));
            titleBar.setTitle(getResources().getString(R.string.tstudy));
        }
        titleBar.setOnLeftListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HomeFragment homeFragment = CommonUtil.getHomeFragment(getActivity());
                homeFragment.fragmentBack();
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HomeFragment homeFragment = CommonUtil.getHomeFragment(getActivity());
                homeFragment.switchContent(new TibetanWordFragment(),list.get(position));
            }
        });
    }

    private void initData(){
        list = RealmUtil.getPhraseByType(4);
        TibetanAdapter tibetanAdapter = new TibetanAdapter(getActivity(),list);
        listView.setAdapter(tibetanAdapter);
    }
}
